package com.ycfmedical.bed.present;

import android.graphics.Color;
import android.os.StrictMode;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.model.ChartManage;
import com.ycfmedical.bed.serverdata.DBUtil;
import com.ycfmedical.bed.ui.fragment.VsFragment;
import com.ycfmedical.bed.utils.MyXFormatter;
import com.ycfmedical.bed.utils.SleepCount;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XPresent;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017-04-07.
 */

public class VSPresenter extends XPresent<VsFragment> {
    private DBUtil dbutil;
    private String Time;
    private String medicalCard = DataManager.getInstance().getUser().getId() + "";
    private HashMap<String ,TextView> texts;


    public VSPresenter() {
        init();
    }

    private void init() {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        dbutil = DBUtil.getInstance();
    }

    public void showSleepData() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                Time = getV().getTime();
                String s1 = Time.replaceAll("年", "-");
                String s2 = s1.replaceAll("月", "-");
                String s3 = s2.replaceAll("日", "");
                List<HashMap<String, String>> list = dbutil.get_data(Constants.NEWTIME, Constants.NEWID);
                ArrayList<Entry> entries = SleepCount.askSleep(list);
                HashMap hashMap = new HashMap();

                hashMap.put("数值",entries);
                hashMap.put("睡眠时长",SleepCount.countSleep(list,entries));
                hashMap.put("平均心率",SleepCount.countAvHeart(list));
                hashMap.put("平均呼吸",SleepCount.countAvBreathe(list));
                hashMap.put("X轴标签",SleepCount.XContText(list));
                if (hashMap != null) {
                    rx.Observable.create(new rx.Observable.OnSubscribe<HashMap>() {
                        @Override
                        public void call(Subscriber<? super HashMap> subscriber) {

                            subscriber.onNext(hashMap);

                        }
                    }).onBackpressureDrop()
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribeOn(Schedulers.immediate())
                            .subscribe(new Subscriber<HashMap>() {
                                @Override
                                public void onCompleted() {
                                    //先判断再解除绑定
                                    if (isUnsubscribed()) {
                                        unsubscribe();
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {
                                    XLog.e("SJH", e.toString());
                                }

                                @Override
                                public void onNext(HashMap data) {
                                    resolve(data);
                                    request(1);
                                }
                            });
                } else {
                    Toast.makeText(getV().getContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }

            }
        }.start();

    }

    private void resolve(HashMap list) {
        ArrayList<Entry> entries = (ArrayList<Entry>) list.get("数值");

        MyXFormatter myXFormatter = new MyXFormatter((String[]) list.get("X轴标签"));
        ChartManage.setXText(getV().getChartManage().getLineCharts().get(3),10,myXFormatter);

        getV().getChartManage().setLineData(3, entries,true);
        getV().getChartManage().setLine(3, Color.rgb(17,116,228), false, LineDataSet.Mode.CUBIC_BEZIER);
        getV().getChartManage().getLineCharts().get(3).getXAxis().setAxisMaximum(entries.size());
        getV().replaceData(3);
        getV().getChartManage().inverted(3,true);
        getV().getAvBreathe().setText(list.get("平均呼吸").toString());
        getV().getAvHeart().setText(list.get("平均心率").toString());
        getV().getAvSleepTime().setText(list.get("睡眠时长").toString());
        showSleepStatus(SleepCount.sleepStatus(entries));
    }

    private void showSleepStatus(Integer [] sleepStatus) {
        if(sleepStatus[0] == null){
            return;
        }
       HashMap<String,TextView> texts = getV().getTexts();
        texts.get("深睡").setText("深睡:"+sleepStatus[0]+"%");
        texts.get("中睡").setText("中睡:"+sleepStatus[1]+"%");
        texts.get("浅睡").setText("浅睡:"+sleepStatus[2]+"%");
        texts.get("清醒").setText("清醒:"+sleepStatus[3]+"%");
    }

    @Override
    protected VsFragment getV() {
        return super.getV();
    }

    @Override
    public void detachV() {
        super.detachV();

    }

}
